// ==========================================================================
// 云捷GO自动生成model扩展代码列表、增、删，改、查、导出，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-07-26 15:16:14
// 生成路径: app/model/module/news/news.go
// 生成人：gfcms
// ==========================================================================
package news

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/os/gtime"
	"myblog/app/utils/page"
)

type EntityExtend struct {
	Entity
	Catename string `json:"catname"` // 栏目名称
}
type ShowExtend struct {
	*Entity
	PreviousPage *Entity
	NextPage     *Entity
}

//新增页面请求参数
type AddReq struct {
	Catid       int    `p:"catid" `
	Title       string `p:"title" v:"required#Title不能为空"`
	Thumb       string `p:"thumb" `
	Description string `p:"description" `
	Author      string `p:"author" `
	Views       int    `p:"views"`
	Zan         int    `p:"zan"`
	Top         int    `p:"top"`
	Indexshow   int    `p:"indexshow"`
	Content     string `p:"content"`
	Listorder   int    `p:"listorder" `
	Status      int    `p:"status"`
}

//修改页面请求参数
type EditReq struct {
	Id          int    `p:"id" v:"required#主键ID不能为空"`
	Catid       int    `p:"catid" `
	Title       string `p:"title" `
	Thumb       string `p:"thumb" `
	Description string `p:"description" `
	Author      string `p:"author" `
	Views       int    `p:"views"`
	Zan         int    `p:"zan"`
	Top         int    `p:"top"`
	Indexshow   int    `p:"indexshow"`
	Content     string `p:"content"`
	Oldimage    string `p:"oldimage"`
	Listorder   int    `p:"listorder" `
	Status      int    `p:"status" v:"required#状态不能为空"`
}

//分页请求参数 
type SelectPageReq struct {
	Title         string `p:"title"`         //标题
	BeginTime     string `p:"beginTime"`     //开始时间
	EndTime       string `p:"endTime"`       //结束时间
	PageNum       int    `p:"pageNum"`       //当前页码
	PageSize      int    `p:"pageSize"`      //每页数
	OrderByColumn string `p:"orderByColumn"` //排序字段
	IsAsc         string `p:"isAsc"`         //排序方式
}

//列表分页请求参数
type ListPageReq struct {
	Catid    int64 `p:"catid" v:"integer#参数必须为整数"`
	PageNum  int   `p:"pagenum" v:"integer#参数必须为整数"` //当前页码
	PageSize int   `p:"pagesize"`                    //每页数
}

//内容页请求参数
type ShowReq struct {
	Newsid int64 `p:"newsid" v:"integer#参数必须为整数"`
}

//todo根据条件分页查询数据

func SelectListByPage(param *SelectPageReq) ([]EntityExtend, *page.Paging, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_news t")
	model.LeftJoin("t_category", "c", "c.id=t.catid")
	if param != nil {

		if param.Title != "" {
			model.Where("t.title like ?", "%"+param.Title+"%")
		}
		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
		model.Order("t.listorder desc")
	}

	total, err := model.Count()

	if err != nil {
		return nil, nil, gerror.New("读取行数失败")
	}

	page := page.CreatePaging(param.PageNum, param.PageSize, total)

	model.Limit(page.StartNum, page.Pagesize)

	if param.OrderByColumn != "" {
		model.Order(param.OrderByColumn + " " + param.IsAsc)
	}

	var result []EntityExtend
	model.Fields("t.*", "c.catname").Structs(&result)
	return result, page, nil
}

// 导出excel
func SelectListExport(param *SelectPageReq) (gdb.Result, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_news t")

	if param != nil {

		if param.Title != "" {
			model.Where("t.title like ?", "%"+param.Title+"%")
		}
		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	//"","栏目名称","标题","缩略图","描述","排序","","跳转链接","创建时间","修改时间",
	model.Fields(" t.id ,t.catid ,t.title ,t.thumb ,t.description ,t.listorder ,t.status ,t.urllink ,t.create_time ,t.update_time")

	result, _ := model.All()
	return result, nil
}

//获取所有数据
func SelectListAll(param *SelectPageReq) ([]Entity, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_news t")

	if param != nil {

		if param.Title != "" {
			model.Where("t.title like ?", "%"+param.Title+"%")
		}
		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	var result []Entity
	model.Scan(&result)
	return result, nil
}

//根据条件分页查询数据
func ListByPage(param *ListPageReq) ([]*Entity, *page.Paging, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_news")
	model.Where("catid=?", param.Catid)
	model.Order("listorder desc")
	total, err := model.Count()
	if err != nil {
		return nil, nil, gerror.New("读取行数失败")
	}
	page := page.CreatePaging(param.PageNum, param.PageSize, total)
	model.Limit(page.StartNum, page.Pagesize)
	var result []*Entity
	model.Scan(&result)
	return result, page, nil
}

func AddViews(newsid int64, ip string) error {
	//	通过ip 验证用户是否已经访问
	db, err := gdb.Instance()
	if err != nil {
		return gerror.New("获取数据库连接失败")
	}
	model := db.Model("t_news_views")
	num, err := model.Count("ip=? and newsid=?", ip, newsid)
	if num == 0 {
		temp := struct {
			Newsid     int64       `orm:"newsid"`
			Ip         string      `orm:"ip"`
			Count      int         `orm:"count"`
			CreateTime *gtime.Time `orm:"create_time"` // 创建时间
		}{
			Newsid:     newsid,
			Ip:         ip,
			Count:      1,
			CreateTime: gtime.Now(),
		}
		res, err := model.Insert(temp)
		if err != nil {
			return err
		}
		num, err := res.LastInsertId()
		if err != nil {
			return err
		}
		//添加记录成功修改内容数据
		if num > 0 {
			db.Model("t_news").Where("id=?", newsid).Update("views=views+1")
		}
	}

	return nil
}

//首页top
func TopIndex(num int) ([]*EntityExtend, error) {
	db, err := gdb.Instance()
	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}
	model := db.Model("t_news t")
	model.Fields("t.*", "c.catname")
	model.Where("t.top=? and t.indexshow=?", 1, 1)
	model.LeftJoin("t_category c", "t.catid = c.id")
	model.Limit(num)
	var result []*EntityExtend
	model.Scan(&result)
	return result, nil
}

//最新文章
func Indexshow(num int) ([]*EntityExtend, error) {
	db, err := gdb.Instance()
	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}
	model := db.Model("t_news t")
	model.Fields("t.*", "c.catname")
	model.Where("t.indexshow=?", 1)
	model.LeftJoin("t_category c", "t.catid = c.id")
	model.Limit(num)
	var result []*EntityExtend
	model.Scan(&result)
	return result, nil
}
